package com.harry.threaddemo;

import java.util.concurrent.TimeUnit;

/**
 * 抛出 InterruptedException 的线程，中断状态被重置为默认状态 false
 * 
 * @author Harry
 * @date 2018/3/22 14:39
 */
public class InterruptedException implements Runnable{

	@Override
	public void run() {
		// 一直 sleep
		try {
			TimeUnit.SECONDS.sleep(10);
		} catch (java.lang.InterruptedException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) throws java.lang.InterruptedException {
		Thread interruptedThread = new Thread(new InterruptedException(), "InterruptedThread");
		interruptedThread.start();

		TimeUnit.SECONDS.sleep(2);

		// 中断被阻塞状态（sleep、wait、join 等状态）的线程，会抛出异常 InterruptedException
		// 在抛出异常 InterruptedException 前，JVM 会先将中断状态重置为默认状态 false
		interruptedThread.interrupt();
		System.out.println("InterruptedThread interrupted is " + interruptedThread.isInterrupted());
		TimeUnit.SECONDS.sleep(2);
	}
}
